//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.BLL.Managers;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Enums;

namespace Tathata.Common.BLL.Entities
{
    /// <summary>
    /// Represents a shopping cart item
    /// </summary>
    public class ShoppingCart : BaseEntity
    {
        #region Ctor
        /// <summary>
        /// Creates a new instance of the shopping cart class
        /// </summary>
        public ShoppingCart()
        {
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the shopping cart identifier
        /// </summary>
        public int ShoppingCartID { get; set; }

        /// <summary>
        /// Gets or sets the shopping cart type identifier
        /// </summary>
        public int ShoppingCartTypeID { get; set; }

        /// <summary>
        /// Gets or sets the customer session identifier
        /// </summary>
        public Guid CustomerSessionGUID { get; set; }

        /// <summary>
        /// Gets or sets the product variant identifier
        /// </summary>
        public int ProductVariantID { get; set; }

        /// <summary>
        /// Gets or sets the product variant attribute identifiers
        /// </summary>
        public List<int> AttributeIDs { get; set; }

        /// <summary>
        /// Gets or sets the text option
        /// </summary>
        public string TextOption { get; set; }

        /// <summary>
        /// Gets or sets the quantity
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// Gets or sets the date and time of instance creation
        /// </summary>
        public DateTime CreatedOn { get; set; }

        /// <summary>
        /// Gets or sets the date and time of instance update
        /// </summary>
        public DateTime UpdatedOn { get; set; }
        #endregion 
        
        #region Custom Properties

        /// <summary>
        /// Gets the log type
        /// </summary>
        public ShoppingCartTypeEnum ShoppingCartType
        {
            get
            {
                return (ShoppingCartTypeEnum)ShoppingCartTypeID;
            }
        }

        /// <summary>
        /// Gets the product variant
        /// </summary>
        public ProductVariant ProductVariant
        {
            get
            {
              return  ProductVariantManager.GetByProductVariantID(ProductVariantID);
            }
        }

        /// <summary>
        /// Gets the product variant attribute values
        /// </summary>
        public ProductVariantAttributeValueCollection Attributes
        {
            get
            {
                ProductVariantAttributeValueCollection pvaValues = new ProductVariantAttributeValueCollection();
                foreach (int attributeID in this.AttributeIDs)
                {
                    ProductVariantAttributeValue pvaValue = ProductVariantAttributeValueManager.GetByProductVariantAttributeValueID(attributeID);
                    if (pvaValue != null)
                        pvaValues.Add(pvaValue);
                }
                return pvaValues;
            }
        }

        /// <summary>
        /// Gets the subtotal with discount
        /// </summary>       
        public decimal SubTotalWithDiscount
        {
            get
            {
                decimal subTotalWithDiscount = decimal.Zero;
                ProductVariant productVariant = ProductVariant;
                if (productVariant != null)
                {
                    decimal attributesTotalPrice = decimal.Zero;
                    foreach (ProductVariantAttributeValue pvaValue in this.Attributes)
                        if (pvaValue.PriceAdjustment > 0)
                            attributesTotalPrice += pvaValue.PriceAdjustment;
                    subTotalWithDiscount = (productVariant.FinalPriceWithDiscount + attributesTotalPrice) * Quantity;
                }
                return subTotalWithDiscount;
            }
        }

        /// <summary>
        /// Gets the subtotal without discount
        /// </summary>   
        public decimal SubTotalWithoutDiscount
        {
            get
            {
                decimal subTotalWithoutDiscount = decimal.Zero;
                ProductVariant productVariant = ProductVariant;
                if (productVariant != null)
                {
                    decimal attributesTotalPrice = decimal.Zero;
                    foreach (ProductVariantAttributeValue pvaValue in this.Attributes)
                        if (pvaValue.PriceAdjustment > 0)
                            attributesTotalPrice += pvaValue.PriceAdjustment;
                    subTotalWithoutDiscount = (productVariant.FinalPriceWithoutDiscount + attributesTotalPrice) * Quantity;
                }
                return subTotalWithoutDiscount;
            }
        }

        /// <summary>
        /// Gets the total weight
        /// </summary> 
        public decimal TotalWeigth
        {
            get
            {
                decimal totalWeigth = decimal.Zero;
                ProductVariant productVariant = ProductVariant;
                if (productVariant != null)
                    totalWeigth = productVariant.Weight * Quantity;
                return totalWeigth;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the shoppung cart item is free shipping
        /// </summary>
        public bool IsFreeShipping
        {
            get
            {
                ProductVariant productVariant = ProductVariant;
                if (productVariant != null)
                    return productVariant.IsFreeShipping;
                return true;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the shoppung cart item is ship enabled
        /// </summary>
        public bool IsShipEnabled
        {
            get
            {
                ProductVariant productVariant = ProductVariant;
                if (productVariant != null)
                    return productVariant.IsShipEnabled;
                return false;
            }
        }

        #endregion
    }

}
